/* 
 * File:   Tuple.h
 * Author: iamani
 *
 * Created on February 23, 2012, 6:36 PM
 */

#ifndef TUPLE_H
#define	TUPLE_H

#include "Mutex.h"
#include "contest_interface.h"

/*Tuple used to add extra info for the record */

class Tuple {
    Record r;
    bool writer_flag;
    bool reader_flag;
    std::set<Transaction*> readers;
    Transaction **writer;

    bool tryinsertReader(Transaction **tx) {
        if (writer_flag) {
            return false;
        }
        return true;
    };

    bool insertReader(Transaction **tx) {
        while (writer_flag) {
            sleep(1);
        }
        reader_flag = true;
        readers.insert(*tx);
        return true;
    };

    bool popReader(Transaction **tx) {
        readers.erase(readers.find(*tx));
        if (readers.size() == 0)
            reader_flag = false;
    }

    bool tryToWrite(Transaction **tx) {
        if (reader_flag) {
            return false;
        }
        return true;
    };

    bool markWriter(Transaction **tx) {
        if (writer_flag) {
            return false;
        }
        writer_flag = true;
        writer = tx;
    };


};

#endif	/* TUPLE_H */
